package ${package}.${moduleName}.service.impl;

import ${package}.${bottomName}.${daoName}.${className}Mapper;
import ${package}.${bottomName}.${entityName}.${className}Entity;
import ${package}.${bottomName}.dto.req.${className}QueryReq;
import ${package}.${bottomName}.dto.req.${className}StoreReq;
import ${package}.${bottomName}.dto.resp.${className}Resp;
import ${package}.${bottomName}.dto.resp.Excel${className}Resp;
import ${package}.${moduleName}.service.${className}Service;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import com.xiangxiang.support.common.excel.EasyExcelUtils;
import com.xiangxiang.support.common.threadlocal.ThreadLocalContext;
import com.xiangxiang.support.common.threadlocal.ThreadLocalContextHolder;
import com.xiangxiang.common.mybatis.common.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import java.time.Instant;
import java.util.List;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.xiangxiang.commonbase.exception.CommonException;
import org.apache.commons.lang3.ObjectUtils;

/**
* ${classname}服务接口
* ${comments}
* @author ${author}
* @date ${datetime}
* 邮箱=-> ${email}
*/
@Service("${classname}Service")
@RequiredArgsConstructor
public class ${className}ServiceImpl extends SuperServiceImpl<${className}Mapper, ${className}Entity> implements ${className}Service{

    private final ${className}Mapper ${classname}Mapper;

    /**
    * 分页查询
    * @param entity 参数
    * @return 分页结果
    */
    @Override
    public IPage<${className}Resp> page(${className}QueryReq entity){
         entity.setIsDeleted(false);
         IPage<${className}Resp> page = new Page<>(entity.getPage(), entity.getPageSize());
         page.orders().add(new OrderItem("create_time",false));
         page.setRecords(${classname}Mapper.page(page,entity));
         return page.convert(t -> BeanUtil.toBean(t, ${className}Resp.class));
    }

    /**
    * 查看详情
    * @param id 主键
    * @return
    */
    @Override
    public ${className}Resp view(${pk.attrType} id){
       return ${classname}Mapper.view(id);
    }

    /**
     * 保存数据
     * @param entity 参数
     * @return 保存结果
     */
     @Override
     @Transactional(rollbackFor = Exception.class)
     public boolean save(${className}StoreReq entity) {
        ${className}Entity pojo=new ${className}Entity();
        BeanUtils.copyProperties(entity,pojo);
        return save(pojo);
     }

     /**
      * 更新数据
      * @param entity 参数
      * @return 更新结果
      */
      @Override
      @Transactional(rollbackFor = Exception.class)
      public boolean update(${className}StoreReq entity) {
          ${className}Resp preData = ${classname}Mapper.view(entity.get${pk.attrName}());
          if (ObjectUtils.isEmpty(preData)) {
             throw new CommonException("数据不存在");
          }
          ${className}Entity pojo=new ${className}Entity();
          BeanUtils.copyProperties(entity,pojo);
          return updateById(pojo);
      }

     /**
     * 删除数据
     * @return 删除结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(${className}StoreReq entity) {
        ${className}Resp preData = ${classname}Mapper.view(entity.get${pk.attrName}());
        if (ObjectUtils.isEmpty(preData)) {
            throw new CommonException("数据不存在");
        }
        return ${classname}Mapper.update(null, new LambdaUpdateWrapper<${className}Entity>()
                    .eq(${className}Entity::get${pk.attrName}, entity.get${pk.attrName}())
                    .set(${className}Entity::getLogicDelete, true)
              )>0;
      }

    /**
    * 导出数据
    * @param response 响应体
    * @param req 请求参数
    */
    @Override
    public void export(HttpServletResponse response, ${className}QueryReq req) {
        req.setPage(1);
        req.setPageSize(Integer.MAX_VALUE);
        List<Excel${className}Resp> dataList= BeanUtil.copyToList(page(req).getRecords(), Excel${className}Resp.class);
        ThreadLocalContextHolder.set(ThreadLocalContext.builder().timezone(req.getTimezone()).build());
        EasyExcelUtils.export(response, "${comments}", "${comments}", Excel${className}Resp.class, dataList);
        ThreadLocalContextHolder.remove();
    }



}
